<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Dayrui Website Management System
 *
 * @since		version 2.0.2
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */

class Selection extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
    }

    /**
    	可以传递以下参数：
    		progress : {
	    		complete 已完成
	    		doing 学习中
	    		all 全部（默认）
    		},
    		type :｛
    			１	: 必修课
    			2 	: 选修课
     		｝
    **/
    public function index() {
	    if($this->member['uid'] < 1) {
		    $this->msg('尚未登陆');
	    }
	    $type = $type ? $type : (int) $this->input->get('type');
	    $progress = $progress ? $progress : $this->input->get('progress');
	    if($progress == 'complete') {
		    $this->link->where('progress',100);
	    } elseif($progress == 'doing') {
		    $this->link->where("progress < 100");
		}

		if($type > 0 && $type < 3) {
			$this->link->where("type", $type);
		}		
	    $video_ids = $this->link->where('mid', $this->member['uid'])->get('member_video_selection')->result_array();	    
	    $lists = array();
	    $points = 0;
	    foreach( $video_ids as $key => $value ) {
		    $vinfo = $this->_show($value['video_id'], 1, TRUE);
		    if(!$vinfo) continue;
	    	$value['title'] = $vinfo[0]['title'];
	    	$value['url'] = $vinfo[0]['url'];
	    	$lists[$key] = $value;
	    	$points += $value['points'];
	    }
	    $this->template->assign('lists',$lists);
	    $this->template->assign('points',$points);
	    $this->template->display('my.html');
    }

    /* 添加选课 */
    public function add() {
	    $id = $id ? $id : (int)$this->input->get('id');
	    $content = $this->content_model->get($id);
	    if(!$content) {
		    exit(dr_json(0, '视频不存在'));
	    }
	    $result = $this->check($id, 'show');
	    if($this->code !== 1) {		    
		    exit(dr_json(0, $this->msg));
	    }

	    if($content['start_time'] && $content['start_time'] > SYS_TIME) {
		    exit(dr_json(0, '本课程尚未开始，赞不能选课'));
	    }
	    if($content['end_time'] && $content['end_time'] < SYS_TIME) {
		    exit(dr_json(0, '本课程已经结束，赞不能选课'));
	    }
	    $extends = $this->link->where('cid', $id)->get(SITE_ID.'_video_extend', 'id')->result_array();
	    if(!$extends) exit(dr_json(0, '没有分集数据'));
	    $video_extends = array();
	    $vieeo_points = 0;
	    foreach( $extends AS $extend) {
		    $video_extends[$extend['id']] = array(
		    	'point' 	=> $extend['point'],
		    	'complete' 	=> 0,
		    );
		    $vieeo_points += (int) $extend['point'];
	    }
	    $selection = array(
	    	'mid' => $this->member['uid'],
	    	'video_id' => $id,
	    	'extra_ids' => json_encode($video_extends),
	    	'progress' => 0,
	    	'points' => $vieeo_points,
	    	'type' => $content['type'],
	    	'dateline' => SYS_TIME,
	    );	       
	    $rs = $this->link->insert('member_video_selection', $selection);
	    if($rs !== TRUE) {
		    exit(dr_json(0, '选课失败'));
	    } else {
		    exit(dr_json(1, '选课成功'));
	    }	    
    }

    public function delete() {
	    $id = $id ? $id : (int)$this->input->get('id');
	    $selection = $this->check($id, 'show');
	    if(!$selection) {
		    $this->msg($this->msg);
	    }
	    /* 如果有观看过 */
	    if($selection['progress'] > 0) {
		    $points = 0;
		    foreach( $selection['extra_ids'] as $key => $value ) {
		    	if($value['complete'] == 1) $points += $value['point'];
		    }
		    if($points > 0) {
			    $this->link->where('uid', $this->member['uid'])->set('points', 'points-'.$points, FALSE)->update('member');
		    }
	    }
	    $r = $this->link->where('id', $selection['id'])->delete('member_video_selection');
	    if($r) {
		    $this->msg('删除成功');
	    } else {
		    $this->msg('删除失败');
	    }
    }

    public function complete() {
	    $id = $id ? $id : (int)$this->input->get('id');
	    $r = $this->check($id);
	    if($r !== false) {
		    $extra = $r['extra_ids'][$id];
		    if($extra['complete'] == 0) {
			    $r_point = $this->link->where('uid', $this->member['uid'])->set('points', 'points+'.$extra['point'], FALSE)->update('member');
			    if($r_point) {
				    $r['extra_ids'][$id]['complete'] = 1;
				    $complete = 0;
				    foreach( $r['extra_ids'] as $v) {
				    	if($v['complete'] == 1) $complete++;
				    }
				    $r['progress'] = round(($complete / count($r['extra_ids']) * 100), 1);
				    $r['extra_ids'] = json_encode($r['extra_ids']);
				    $this->link->where('id', $r['id'])->set('extra_ids', $r['extra_ids'], TRUE)->set('progress', $r['progress'], false)->update('member_video_selection');
			    }
		    }
		    exit(dr_json(1, '积分增加成功'));
	    } else {
		    exit(dr_json(0, $this->msg));
	    }
    }
}